package com.wyp.didishige.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wyp.didishige.R;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.db.NewComment;
import com.wyp.didishige.utils.DateUtil;
import com.wyp.didishige.utils.ImageLoadOptions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by horrywu on 16/6/14.
 * 评论列表适配器
 */
public class NewCommentAdapter extends BaseListAdapter<NewComment> {
    public NewCommentAdapter(Context context, List<NewComment> bmobIMMessageList) {
        super(context, bmobIMMessageList);
    }

    @Override
    public View bindView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_new_comment, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((NewComment) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    @SuppressWarnings("MalformedFormatString")
    private void initializeViews(NewComment object, ViewHolder holder) {
        String avatar = object.getAvatar();
        if (avatar != null && !avatar.equals("")) {
            ImageLoader.getInstance().displayImage(avatar, holder.avatar,
                    ImageLoadOptions.avatarOptions);
        } else {
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_launcher, holder.avatar,
                    ImageLoadOptions.avatarOptions);
        }
        holder.name.setText(object.getName());
        String comment = "";
        if (object.getType() == 0) {
            comment = "回复了你的动态：" + object.getContent();
        } else {
            comment = "回复了你的评论：" + object.getContent();
        }
        holder.comment.setText(comment);
        if ((object.getStatus() != null ? object.getStatus() : Config.STATUS_VERIFY_NONE) == Config.STATUS_VERIFY_READED) {
            holder.comment.setTextColor(mContext.getResources().getColor(R.color.grey));
            holder.name.setTextColor(mContext.getResources().getColor(R.color.grey));
        } else {
            holder.comment.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.name.setTextColor(mContext.getResources().getColor(R.color.black));
        }
        holder.time.setText(String.format("%s", DateUtil.getDateSpan((object.getTime() == null ? System.currentTimeMillis() : object.getTime()) / 1000,
                DateUtil.getTimestamp(new Date()))));
    }

    protected class ViewHolder {
        private ImageView avatar;
        private TextView name;
        private TextView comment;
        private TextView time;

        public ViewHolder(View view) {
            avatar = (ImageView) view.findViewById(R.id.avatar);
            name = (TextView) view.findViewById(R.id.name);
            comment = (TextView) view.findViewById(R.id.comment);
            time = (TextView) view.findViewById(R.id.time);
        }
    }
}
